<?php
/*------------------------------------------------------------------------------ 
    Create Date :   25/10/2010
    Author    :      Nguyen Hoang Phuc
    Copyright(c) 2010 A.D.A Solution. All rights reserved    
    ------------------------------------------------------------------------------
    Update History:
    Ver.    TRB#             Date          Author       Note
    Lephan  TRB1           2010/01/20      HoangPhuc    Xử lý xem bản đồ trường hợp Phương tiện có check tổng hợp
------------------------------------------------------------------------------*/
if (!defined('IN_ADAGPS')){
    die(SYSTEM_ACCESS_DENIED);
} 

global $objTemplate, $objDbSelect,$arrConfig;   

// dang ky su dung ham xajax   
$objXajax->registerFunction('ajxUpdateData');
$objXajax->registerFunction('ajxLoadData');
// load dữ liệu
function ajxLoadData($load = 0)
{
   global $objDbSelect,$objTemplate;
   try
  {
   $now = date('Y-m-d');
   $SQL = "DELETE FROM tbl_current_employee WHERE createdate != '$now'";
   $rsd = $objDbSelect->Execute($SQL);    
   // load danh sach nhân viên 
   $sql = "SELECT id,username,url_image FROM tbl_employee WHERE id not in (SELECT id_employee FROM tbl_current_employee)";   
   $xjax_respone = new xajaxResponse();
   $rs = $objDbSelect->Execute($sql);
   $sScript1 = 'var arrDataDiv = new Array();'; 
   $i=0;
   if($rs->RecordCount() > 0)
   {
       do
       {
            $id = $rs->fields['id']; 
            $url_image = $rs->fields['url_image'];
            $username = $rs->fields['username'];
            $sScript1.="arrDataDiv[".$i."] = new Array();";
            $sScript1.="arrDataDiv[".$i."][0] = '".$id."';";
            $sScript1.="arrDataDiv[".$i."][1] = '".$url_image."';";
            $sScript1.="arrDataDiv[".$i."][2] = '".$username."';";
            $sScript1.="arrDataDiv[".$i."][3] = 'mydivclass1';"; 
            $sScript1.="arrDataDiv[".$i."][4] =  'myimgclass1';"; 
            $sScript1.="arrDataDiv[".$i."][5] =  'myH5Class1';"; 
            $i++;   
       }                                           
       while($rs->MoveNext());    
   }   
   // load nhan vien nghi lam
       // trash 1 
       $sql = "SELECT e.id,e.username,e.url_image FROM tbl_employee e INNER JOIN tbl_current_employee c ON e.id = c.id_employee WHERE c.`status` = 0";      
       $rs = $objDbSelect->Execute($sql);
       $sScript2 = 'var arrDataDiv2 = new Array();';
       $i=0;  
       if($rs->RecordCount() > 0)
       {
           do
           {
                $id_image = $rs->fields['id'];
                $url_image = isset($rs->fields['url_image']) ? $rs->fields['url_image'] : ''; 
                $username = $rs->fields['username'];  
                if ($url_image != '')
                {
                  $sScript2.="arrDataDiv2[".$i."] = new Array();";
                  $sScript2.="arrDataDiv2[".$i."][0] = '".$id_image."';";
                  $sScript2.="arrDataDiv2[".$i."][1] = '".$url_image."';";
                  $sScript2.="arrDataDiv2[".$i."][2] = '".$username."';";
                  $sScript2.="arrDataDiv2[".$i."][3] = 'mydivclass2';"; 
                  $sScript2.="arrDataDiv2[".$i."][4] =  'myimgclass2';"; 
                  $sScript2.="arrDataDiv2[".$i."][5] =  'myH5Class2';";
                 $i++;   
                }
           }                                           
           while($rs->MoveNext());    
       }
       // danh sach nhan vien di lam 
       $sql = "SELECT e.id,e.username,e.url_image FROM tbl_employee e INNER JOIN tbl_current_employee c ON e.id = c.id_employee WHERE c.`status` = 1";      
       $rs = $objDbSelect->Execute($sql);
       $sScript3 = 'var arrDataDiv3 = new Array();'; 
       $i=0; 
       if($rs->RecordCount() > 0)
       {
           do
           {
                $id_image = $rs->fields['id'];
                $url_image = isset($rs->fields['url_image']) ? $rs->fields['url_image'] : '';  
                $sScript .= '';
                $username = $rs->fields['username']; 
                if ($url_image != '')
                {
                  $sScript3.="arrDataDiv3[".$i."] = new Array();";
                  $sScript3.="arrDataDiv3[".$i."][0] = '".$id_image."';";
                  $sScript3.="arrDataDiv3[".$i."][1] = '".$url_image."';";
                  $sScript3.="arrDataDiv3[".$i."][2] = '".$username."';";
                  $sScript3.="arrDataDiv3[".$i."][3] = 'mydivclass2';"; 
                  $sScript3.="arrDataDiv3[".$i."][4] =  'myimgclass2';"; 
                  $sScript3.="arrDataDiv3[".$i."][5] =  'myH5Class2';";
                  $i++;    
                }
           }                                           
           while($rs->MoveNext());    
       }
      $sql = "SELECT e.id,e.username,e.url_image FROM tbl_employee e INNER JOIN tbl_current_employee c ON e.id = c.id_employee WHERE c.`status` = 2";      
       $rs = $objDbSelect->Execute($sql);
       $sScript4 = 'var arrDataDiv4 = new Array();';  
       if($rs->RecordCount() > 0)
       {
           $i = 0;
           do
           {
                $id_image = $rs->fields['id'];
                $url_image = isset($rs->fields['url_image']) ? $rs->fields['url_image'] : '';
                $username = $rs->fields['username'];   
                $sScript .= '';
                if ($url_image != '')
                {
                  $sScript4.="arrDataDiv4[".$i."] = new Array();";
                  $sScript4.="arrDataDiv4[".$i."][0] = '".$id_image."';";
                  $sScript4.="arrDataDiv4[".$i."][1] = '".$url_image."';";
                  $sScript4.="arrDataDiv4[".$i."][2] = '".$username."';";
                  $sScript4.="arrDataDiv4[".$i."][3] = 'mydivclass2';"; 
                  $sScript4.="arrDataDiv4[".$i."][4] =  'myimgclass2';"; 
                  $sScript4.="arrDataDiv4[".$i."][5] =  'myH5Class2';";
                  $i++;    
                }
           }                                           
           while($rs->MoveNext());    
       }
    $xjax_respone->addScript(' $(document).ready(function(){
                     '.$sScript1.'
                     '.$sScript2.'
                     '.$sScript3.'
                     '.$sScript4.'
                     newDrag_Drop.CreateDivTrash(arrDataDiv,"gallery",1);
                     newDrag_Drop.CreateDivTrash(arrDataDiv2,"trash1",2);
                     newDrag_Drop.CreateDivTrash(arrDataDiv3,"trash2",2);
                     newDrag_Drop.CreateDivTrash(arrDataDiv4,"trash3",2);                
                     newDrag_Drop.DragDropGalleryAllEmployee();
                     newDrag_Drop.CreateDragDropTrash($("#trash1"),0);
                     newDrag_Drop.CreateDragDropTrash($("#trash2"),1);
                     newDrag_Drop.CreateDragDropTrash($("#trash3"),2);
                   });');           
   return $xjax_respone -> getXML(); 
  }                 
   catch (Exception $e) {
         // $xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }      
}
// trường hợp nhấn stop
function ajxUpdateData($id_employee,$status)
{
  global $objDbSelect;
   try
   {
   $xjax_respone = new xajaxResponse();
   $SQL = ""; 
   if ($status == -1)
   {
     $SQL = "DELETE FROM tbl_current_employee WHERE id_employee = $id_employee";   
   }
   else
   {
       $sSQL2 = "";
       $sql = "SELECT id FROM tbl_current_employee  WHERE id_employee = $id_employee";  
//       print_r($sql);
//       die();    
       $rs = $objDbSelect->Execute($sql);  
       $time = date('Y-m-d H:i');
       $now = date('Y-m-d');
       $timestop = "";
        if ($status == 1)
        {
         $timestop = "0000-00-00 00:00:00"; 
        }
        else
        {
         $timestop = $time;   
        }
       if($rs->RecordCount() > 0)
        {
            
            $SQL = "UPDATE tbl_current_employee SET `status` = $status WHERE id = ".$rs->fields['id']; 
            $sSQL2 = "UPDATE tbl_history_employee SET `status` = $status,endtime = '$timestop' WHERE id_employee = $id_employee AND DATE_FORMAT(createdate,'%Y-%m-%d') = '$now'";   
        }
       else
        {
           $SQL = "INSERT INTO tbl_current_employee(id_employee,`status`,createdate) VALUES($id_employee,$status,'$now')";
           $sSQL2 = "INSERT INTO tbl_history_employee(id_employee,`status`,createdate,endtime) VALUES($id_employee,$status,'$time','$timestop')";        
        }
     $objDbSelect->Execute($sSQL2);    
   }
   $objDbSelect->Execute($SQL); 
   }
   catch (Exception $e) {
         // $xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }      
  return $xjax_respone -> getXML();             
}   

?>
